package e.g.com.pt2acqlc.client;

import e.g.com.pt2acqlc.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.AbsolutePanel;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Pt2acqlc implements EntryPoint {

	private GestionnaireDeConnexion gdc;
	private RootPanel rootPanel;
	private int i = 0 ;
	/**
	 * This is the entry point method.
	 * @wbp.parser.entryPoint
	 */
	public void onModuleLoad() {
		final Button connection = new Button("Connexion");
		connection.setText("Connexion");
		loginField = new TextBox();
		loginField.setText("Pseudo");
		
		pswdField = new TextBox();
		lblPseudo_1 = new Label();
		lblPseudo_1.setText("Pseudo : ");
		final Label lblMotDePasse = new Label();
		lblMotDePasse.setText("Mot de passe :");
		pswdField.setText("Pasword");

		// We can add style logins to widgets
		connection.addStyleName("connectionButton");

		// Add the loginField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		rootPanel = RootPanel.get("pseudoFieldContainer");
		rootPanel.add(loginField, 1155, 247);
		
		rootPanel.add(lblPseudo_1, 1052, 247);
		rootPanel.add(pswdField, 1155, 304);
		
		rootPanel.add(pswdField, 1155, 304);
		rootPanel.add(lblMotDePasse, 1013, 304);
		rootPanel.add(connection, 1101, 407);
		
		
		
		// Focus the cursor on the login field when the app loads
		loginField.setFocus(true);		
		
		Label lbinscription = new Label("S'inscrire");
		rootPanel.add(lbinscription, 1155, 362);
		
		lblErreur = new Label("");
		rootPanel.add(lblErreur, 1052, 464);
		loginField.selectAll();
		lbinscription.addClickHandler(new ClickHandler() {
		@Override
        public void onClick(ClickEvent event) {
        	rootPanel.clear();
        	afficherInscription();			            
        	}
		});
		
		pswdField.addKeyUpHandler(new KeyUpHandler() {
			 @Override
			 public void onKeyUp(KeyUpEvent event) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
		    			if ((loginField.getText() == "") || (loginField.getText() == "Pseudo") || (pswdField.getText() == "") || (pswdField.getText() == "Pasword")) {
			    			MessageInfoBox.afficher("Identifiant ou mot de passe invalide");	
		    			}
		    			else{
	            	rootPanel.clear();
	            	afficheMenu();			           
	            	}      
					};
			 }
		});
		
		
		connection.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	
            	connexion();
            	}           
        });
        
	}
	
		private static final String SERVER_ERROR = "An error occurred while "
				+ "attempting to contact the server. Please check your network "
				+ "connection and try again.";

		/**
		 * Create a remote service proxy to talk to the server-side Greeting service.
		 */
		private final GreetingServiceAsync greetingService = GWT
				.create(GreetingService.class);
		private TextBox loginField;
		private Label lblPseudo_1;
		private TextBox pswdField;
		private Label lblErreur;

		/**
		 * This is the entry point method.
		 * @wbp.parser.entryPoint
		 */
		   private static class MessageInfoBox extends DialogBox {
			    private MessageInfoBox(String message) {
			      setText("Erreur");
			      final DockPanel panel = new DockPanel();
			      panel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
			      panel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
			      panel.setStyleName("alignement-gauche");
			      panel.add(new Label(message), DockPanel.CENTER);      
			      panel.add(new Image("imgages/information.jpg"), DockPanel.WEST);
			      
			      SimplePanel panelBouton = new SimplePanel();
			      panelBouton.setStyleName("alignement-droite");
			      Button boutonOk = new Button("OK");
			      boutonOk.setWidth("120px");
			      boutonOk.addClickHandler(new ClickHandler() {
			        public void onClick(ClickEvent event) {
			          MessageInfoBox.this.hide();
			        }
			      });
			      panelBouton.add(boutonOk);
			      panel.add(panelBouton, DockPanel.SOUTH);
			      setWidget(panel);
			    }
			    
			    public static void afficher(String message) {
			      new MessageInfoBox(message).center();
			    }
			  };

/////////////////////////////////////////////////////////////////////////////////////	
			   private static class ModifInfoBox extends DialogBox {
				    private ModifInfoBox(String message) {
				      setText("Modification");
				      final DockPanel panel = new DockPanel();
				      panel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
				      panel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
				      panel.setStyleName("alignement-gauche");
				      panel.add(new Label(message), DockPanel.CENTER);      
				      panel.add(new Image("imgages/information.jpg"), DockPanel.WEST);
				      
				      SimplePanel panelBouton = new SimplePanel();
				      panelBouton.setStyleName("alignement-droite");
				      SimplePanel panelBoutonn = new SimplePanel();
				      panelBoutonn.setStyleName("alignement-gauche");
				      
				      Button boutonAnnuler = new Button("Annuler");
				      boutonAnnuler.setWidth("120px");
				      boutonAnnuler.addClickHandler(new ClickHandler() {
				        public void onClick(ClickEvent event) {
				        	ModifInfoBox.this.hide();
				        }
				      });
				      panelBouton.add(boutonAnnuler);
				      
				      Button boutonConfirmer = new Button("Confirmer");
				      boutonConfirmer.setWidth("120px");
				      boutonConfirmer.addClickHandler(new ClickHandler() {
				        public void onClick(ClickEvent event) {
				        	ModifInfoBox.this.hide();
				        }
				      });
				      panelBoutonn.add(boutonConfirmer);
				      
				      panel.add(panelBoutonn, DockPanel.SOUTH);
				      setWidget(panel);
				    }
				    
				    public static void afficher(String message) {
				      new ModifInfoBox(message).center();
				    }
				  };

	/////////////////////////////////////////////////////////////////////////////////////	
        public void afficheProfil() {
        	 Button btnQuitter = new Button("Revenir au menu principal");
             rootPanel.add(btnQuitter, 566, 574);
             btnQuitter.addClickHandler(new ClickHandler() {
         		@Override
         		public void onClick(ClickEvent event) {
         		rootPanel.clear();
         		afficheMenu(); }
         		});
             
             Label lblPseudo = new Label("Pseudo :");
             rootPanel.add(lblPseudo, 564, 195);
             lblPseudo.setSize("59px", "29px");
             
             Label lblPseudoo = new Label();
             rootPanel.add(lblPseudoo, 630, 195);
             lblPseudoo.setSize("163px", "17px");
             
             Button btnChangePseudo = new Button("Change");
             rootPanel.add(btnChangePseudo, 817, 195);
             btnChangePseudo.addClickHandler(new ClickHandler() {
	    		@Override
	    		public void onClick(ClickEvent event) {
	    			ModifInfoBox.afficher("Changement de pseudo");
	    		}});
 			
             Label lblMotDePasse = new Label("Mot de Passe :");
             rootPanel.add(lblMotDePasse, 528, 248);
             
             Label lblBoxMdp = new Label();
             rootPanel.add(lblBoxMdp, 630, 240);
             lblBoxMdp.setSize("163px", "18px");
             
             Button btnChangeMdP = new Button("Change");
             rootPanel.add(btnChangeMdP, 817, 240);
          // Make a command that we will execute from Open and Input.
             Label lblAdresseEmail = new Label("Adresse e-mail :");
             rootPanel.add(lblAdresseEmail, 522, 290);
             lblAdresseEmail.setSize("104px", "30px");
             
             Label lblMail = new Label();
             rootPanel.add(lblMail, 630, 290);
             lblMail.setSize("163px", "18px");
             
             Button ButtonChangeMail = new Button("Change");
             rootPanel.add(ButtonChangeMail, 817, 290);
             ButtonChangeMail.setSize("60px", "29px");


             Label label = new Label("------------------------------------------------------------------------------------------");
             rootPanel.add(label, 453, 327);
             label.setSize("450px", "34px");

             Label lblInformationsProfil = new Label("Informations profil");
             rootPanel.add(lblInformationsProfil, 606, 162);
             lblInformationsProfil.setSize("138px", "25px");      

             Label lblNombreDeParties = new Label("Nombre de parties jouees :");
             rootPanel.add(lblNombreDeParties, 464, 415);
             
             Label lblNbParti = new Label();
             rootPanel.add(lblNbParti, 677, 403);
             lblNbParti.setSize("94px", "17px");

             Label lblPourcentageDeVictoire = new Label("Pourcentage de victoire :");
             rootPanel.add(lblPourcentageDeVictoire, 464, 459);
             
             Label lblPourcent = new Label();
             rootPanel.add(lblPourcent, 681, 447);
             lblPourcent.setSize("90px", "17px");

             Label lblStatistiques = new Label("Statistiques en mode multi-joueurs");
             rootPanel.add(lblStatistiques, 566, 353);
             
             Label lblBoxStat = new Label();
             rootPanel.add(lblBoxStat, 677, 489);
             lblBoxStat.setSize("94px", "17px");

             Label lblNombreDePoints = new Label("Nombre de points en moyenne :");
             rootPanel.add(lblNombreDePoints, 453, 501);
                   
           }
/////////////////////////////////////////////////////////////////////////////////////
		public void afficherInscription(){
			
			Label lblNom = new Label("Nom :");
			rootPanel.add(lblNom, 365, 40);
			
			Label lblPrenom = new Label("Prenom :");
			rootPanel.add(lblPrenom, 346, 90);
			
			Label lblAdresseMail = new Label("Adresse Mail :");
			rootPanel.add(lblAdresseMail, 320, 154);
			
			Label lblMotDePasse = new Label("Mot de Passe :");
			rootPanel.add(lblMotDePasse, 310, 217);
			
			Label lblConfirmezVotreMot = new Label("Confirmez votre mot de passe :");
			rootPanel.add(lblConfirmezVotreMot, 204, 271);
			
			Button btnAnnuler = new Button("Annuler");
			rootPanel.add(btnAnnuler, 293, 343);
			btnAnnuler.addClickHandler(new ClickHandler() {
	    		@Override
	    		public void onClick(ClickEvent event) {
	    		rootPanel.clear();
	    		onModuleLoad(); }
	    		});
	        

						
			final TextBox textBoxNom = new TextBox();
			rootPanel.add(textBoxNom, 423, 37);
			
			final TextBox textBoxPrenom = new TextBox();
			rootPanel.add(textBoxPrenom, 423, 90);
			
			final TextBox textBoxMail = new TextBox();
			rootPanel.add(textBoxMail, 423, 154);
			
			final TextBox textBoxMdp = new TextBox();
			rootPanel.add(textBoxMdp, 423, 217);
	        
			final TextBox textBoxValidMdp = new TextBox();
			rootPanel.add(textBoxValidMdp, 423, 273);
			
			Button btnInscription = new Button("S'inscrire");
			rootPanel.add(btnInscription, 423, 343);
			btnInscription.addClickHandler(new ClickHandler() {
		    		@Override
		    		public void onClick(ClickEvent event) {
		    			if ((textBoxNom.getText() == "") || (textBoxNom.getText() == "") || (textBoxPrenom.getText() == "") || (textBoxMail.getText() == "") || (textBoxMdp.getText() == "") || (textBoxValidMdp.getText()=="")) {
		    					        MessageInfoBox.afficher("Tous les champs sont obligatoires !");
		    			}
		    			else if(textBoxMdp.getText() != textBoxValidMdp.getText()){
	    					        MessageInfoBox.afficher("Mot de passe invalide");
	    					        textBoxMdp.setText("");
	    					        textBoxValidMdp.setText("");
		    					}
		    			else{
		    		rootPanel.clear();
		    		onModuleLoad(); };}
		    		});
	}
/////////////////////////////////////////////////////////////////////////////////////
	public void afficheMenu(){
		// Add the loginField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		Button btnSolo = new Button("solo");
		btnSolo.setText("Partie Solo");
		rootPanel.add(btnSolo, 808, 207);
		btnSolo.setSize("261px", "34px");
		btnSolo.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	rootPanel.clear();
            	afficheJeu();			            
            	}
        });
		
		Button btnMulti = new Button("multi");
		btnMulti.setText("Partie Multijoueur");
		rootPanel.add(btnMulti, 808, 271);
		btnMulti.setSize("261px", "34px");
		
		Button btnProfil = new Button("profil");
		btnProfil.setText("Profil");
		rootPanel.add(btnProfil, 808, 335);
		btnProfil.setSize("261px", "34px");
		btnProfil.addClickHandler(new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
		rootPanel.clear();
		afficheProfil(); }
		});
		
		Button btnRegle = new Button("regles du jeu");
		btnRegle.setText("Règles du jeu");
		rootPanel.add(btnRegle, 808, 399);
		btnRegle.setSize("261px", "34px");
		btnRegle.addClickHandler(new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
		rootPanel.clear();
		//afficheRegle();
		}
		});
		
		Button btnQuitter = new Button("quitter");
		btnQuitter.setText("Quitter");
		rootPanel.add(btnQuitter, 808, 463);
		btnQuitter.setSize("261px", "34px");
		btnQuitter.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	rootPanel.clear();
            	onModuleLoad();			            }
        });
		
		
		Button btnDconnection = new Button("deconnection");
		rootPanel.add(btnDconnection, 1222, 573);
		btnDconnection.setSize("97px", "30px");
		btnDconnection.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	rootPanel.clear();
            	onModuleLoad();			            }
        });
		
	}
	
	/////////////////////////////////////////////////////////////////////////////////////
	public void afficheJeu(){
		
		   
		HTML themeHtml = new HTML("<h2>Theme : </h2>", true);
		rootPanel.add(themeHtml, 698, 244);
		themeHtml.setSize("167px", "34px");
		
		HTMLPanel questionPanel = new HTMLPanel("<h1>Question :                          </h1>");
		rootPanel.add(questionPanel, 656, 338);
		questionPanel.setSize("247px", "49px");
		
		
		final Button btnRep1 = new Button("Reponse 1");	
		rootPanel.add(btnRep1, 687, 460);
		btnRep1.setSize("201px", "40px");
	
		
		final Button btnRep2 = new Button("Reponse 2");
		rootPanel.add(btnRep2, 687, 523);
		btnRep2.setSize("201px", "40px");
		
		
		final Button btnRep3 = new Button("Reponse 3");
		rootPanel.add(btnRep3, 906, 460);
		btnRep3.setSize("201px", "40px");
		
		
		final Button btnRep4 = new Button("Reponse 4");
		rootPanel.add(btnRep4, 906, 523);
		btnRep4.setSize("201px", "40px");	
		
		
		btnRep1.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	btnRep1.addStyleName("buttonStyleClick");     
            	btnRep2.addStyleName(""); 
            	btnRep3.addStyleName(""); 
            	btnRep4.addStyleName(""); 
            }
        });
		
		btnRep2.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	btnRep1.addStyleName("");     
            	btnRep2.addStyleName("buttonStyleClick"); 
            	btnRep3.addStyleName(""); 
            	btnRep4.addStyleName("");            	
            }
        });
		
		btnRep3.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	btnRep1.addStyleName("");     
            	btnRep2.addStyleName(""); 
            	btnRep3.addStyleName("buttonStyleClick"); 
            	btnRep4.addStyleName("");            	
            }
        });
		
		btnRep4.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	btnRep1.addStyleName("");     
            	btnRep2.addStyleName(""); 
            	btnRep3.addStyleName(""); 
            	btnRep4.addStyleName("buttonStyleClick");          	
            }
        });
		
		Label lblRepValide = new Label("Bonne r\u00E9ponse/Mauvaise r\u00E9ponse");
		rootPanel.add(lblRepValide, 797, 585);
		
		HTMLPanel scorePanel = new HTMLPanel("<h1> Score :   </h1>");
		rootPanel.add(scorePanel, 948, 646);
		scorePanel.setSize("136px", "145px");
		
		Button btnQuitter = new Button("Quitter");
		rootPanel.add(btnQuitter, 906, 810);
		btnQuitter.setSize("97px", "30px");
		btnQuitter.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
            	rootPanel.clear();
            	i=10;
            	afficheMenu();			            }
        });
		
		
		
		 // Create a new timer
		
	    Timer t = new Timer() {
	      public void run() {
	    	 Timer t1 = new Timer() {
	    		public void run(){
	    			//afficher la bonne r�ponce en vert
	    			
	    			//si la mauvaise r�ponce a �t� selection� : l'afficher en rouge
	    			//afficher le label de r�ponse valide/invalide
	    		}
	    	  };
	    	  t1.schedule(2000);
	    	  //
	    	  if(i<2){
	    		  rootPanel.clear();
	    		  afficheJeu();
	    		  i=i+1;
	    	  }
	    	  else{
	    		  rootPanel.clear();
	    		  afficheMenu();
	    	  }
	      }
	    };

	    // Schedule the timer to run once in 10 seconds.
	    t.schedule(10000);
	    
	  
	}
	private void connexion() {
		// First, we validate the input.
		//errorLabel.setText("");
		String textToServer ="colin";
		//String textToServer = loginField.getText();
		if (!FieldVerifier.isValidName(textToServer)) {
			lblErreur.setText("Please enter at least four characters");
			return;
		}
		// Then, we send the input to the server.
		greetingService.getPseudo(loginField.getText(),pswdField.getText(),
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						lblErreur.setText("le mot de passe est invalide ou le pseudo n'existe pas");
					}
					public void onSuccess(String result) {
						loginField.setText(result);
						if(result!=""){
						rootPanel.clear();	
						afficheMenu();
						}
						else{
							lblErreur.setText("le mot de passe est invalide ou le pseudo n'existe pas");
						}
						
					}
				});
	}
	}
